package xiangMu.auto.auto02;

public class RentalCarsCompany {
    private String name;    //汽车租赁公司名字
    private String address;
    private String phone;

    private Auto[] autos = new Auto[8];   
    private int autoCount = autos.length;  

    private RentHistory[] histories = new RentHistory[30];    //车辆租赁记录数组，引用型对象数组
    private int historyCount = 0;   

    public RentalCarsCompany(String name, String address, String phone) {
        this.name = name;
        this.address = address;
        this.phone = phone;
     
        autos[0] = new Car("轿车", "宝马", "550i", "京CNY28588", 5, 600);
        autos[1] = new Car("轿车", "宝马", "X6", "京NY28588", 5, 800);
        autos[2] = new Car("轿车", "别克", "林荫大道", "京NT37465", 5, 300);
        autos[3] = new Car("轿车", "别克", "GL8", "京NT96968", 5, 600);
        autos[4] = new Bus("客车", "金杯", "中客", "京6566754", 16, 800);
        autos[5] = new Bus("客车", "金杯", "大客", "京9696996", 34, 1500);
        autos[6] = new Bus("客车", "金龙", "中客", "京8696997", 16, 800);
        autos[7] = new Bus("客车", "金龙", "大客", "京8696998", 34, 1600);
    }


    public Auto rentAuto(String licencePlate, String type, int days) {    //车辆租赁
        Auto auto = find(licencePlate);  
        if (auto == null) {
            System.out.println("没有这辆车");
            return null;
        }
        if (!auto.isState()) {      //车的租赁状态，默认为true,未出租状态
            System.out.println("此车已经租出去了");
            return null;
        }
        if (!auto.getType().equals(type)) {  
            System.out.println("你选择车牌号对应汽车类型不是" + type);
            return null;
        }

        auto.setState(false);   //车辆设为已出租状态

        RentHistory rh = new RentHistory();
        rh.setType(type);
        rh.setBrand(auto.getBrand());
        rh.setSeries(auto.getSeries());
        rh.setLicencePlate(licencePlate);
        rh.setDays(days);
        rh.setSumRent(auto.calcRent(days));
        addRentHistory(rh); //调用添加汽车租赁记录的方法

        return auto;
    }


    /**
     * @param licencePlate 车牌号
     */
    public boolean returnAuto(String licencePlate) {   //车辆归还
        Auto auto = find(licencePlate);  
        if (auto == null) {
            System.out.println("没有这辆车");
            return false;
        }
        if (auto.isState()) {      //车的租赁状态，默认为true,未出租状态
            System.out.println("此车辆并未出租");   
            return false;
        }
        auto.setState(true);   
        return true;
    }


    public boolean delAuto(String licencePlate, String type) {   //车辆报废
        Auto auto = find(licencePlate);  
        if (auto == null) {
            System.out.println("没有这辆车");
            return false;
        }
        if (!auto.isState()) {     
            System.out.println("此车已经租出去了");
            return false;
        }
        if (!auto.getType().equals(type)) {  
            System.out.println("你选择要报废的车牌号对应汽车类型不是" + type);
            return false;
        }

        for (int i = 0; i < autoCount; i++) {
            if (auto == autos[i]) { //对象的引用值相等
                for (int j = i; j < autoCount - 1; j++)
                    autos[j] = autos[j + 1];
                autos[--autoCount] = null;
                return true;
            }
        }
        return false;   
    }


    public void addAuto(Auto auto) {     //采购新车
        expandAuto();    //检查是否需要扩容
        autos[autoCount++] = auto;
        System.out.println("添加新车成功");
    }


    public void getAutoRecord() {  //查看租赁记录
        for (int i = 0; i < historyCount; i++) {
            System.out.println(histories[i]);
        }
        if (histories[0] == null)
            System.out.println("暂无车辆租赁记录");
    }


    public void addRentHistory(RentHistory history) {    //添加汽车租赁记录
        expandRentHistory();    //检查是否需要扩容
        histories[historyCount++] = history;
    }


    public void expandAuto() {   //给车辆对象数组扩容
        if (autoCount >= autos.length) {
            Auto[] autos2 = new Auto[autos.length * 2];
            for (int i = 0; i < autoCount; i++) {
                autos2[i] = autos[i];
            }
            autos = autos2;
        }
    }


    public void expandRentHistory() {   //给租赁记录对象数组扩容
        if (historyCount >= histories.length) {
            RentHistory[] histories2 = new RentHistory[histories.length * 2];
            for (int i = 0; i < historyCount; i++) {
                histories2[i] = histories[i];
            }
            histories = histories2;
        }
    }


    public int getsumAuto() {   //计算多少辆汽车可出租
        int number = 0;
        for (int i = 0; i < autoCount; i++) {
            if (autos[i].isState())
                number++;
        }
        return number;
    }


    /**
     * 辅助方法：
     * 显示对应类型的未出租的车辆信息
     *
     * @param type 车的类型(轿车或客车)
     */
    public void showType(String type) {     //汽车租赁时，输入车辆类型(轿车/客车)后，输出该类型处于可租状态的车辆
        for (int i = 0; i < autoCount; i++) {
            if (autos[i].getType().equals(type) && autos[i].isState())  //默认状态为true，未出租
                System.out.println(autos[i]);
        }
    }


    public void showTypeAll(String type) {     //查看车辆情况时，输入车辆类型(轿车/客车)后，输出该类型所有的车辆(不考虑出租状态)
        for (int i = 0; i < autoCount; i++) {
            if (autos[i].getType().equals(type))
                System.out.println(autos[i]);
        }
    }


    /**
     * 辅助方法：
     * 根据车牌号找到车辆
     *
     * @param licencePlate 车牌号
     * @return
     */
    public Auto find(String licencePlate) { 
        for (int i = 0; i < autoCount; i++) {
            if (autos[i].getLicencePlate().equals(licencePlate))
                return autos[i];
        }
        return null;
    }


    @Override
    public String toString() {
        return name + "欢迎您，请致电" + phone + "洽谈相关租赁业务";
    }

}

